<?php
namespace App\Service\H5;

class IndexService20250921 extends BaseService
{
    //首页列表
    public static function index($pas)
    {
        $db = self::dbConn(self::$dbYwWh)->where('del', 0);
        if (isset($pas['depart']) && $pas['depart_id']) {
            $db = $db->where('depart_id', $pas['depart_id']);
        }
        $db = $db->whereIn('status', [2,3,4,5]);
        $res = $db->get();
        if (!count($res)) {
            return self::toback(-2, '没有数据');
        }
        $statuss = self::$whStatuss;
        $arys = [];
        foreach ($res as $v) {
            $status_name = array_key_exists($v->status, $statuss) ? $statuss[$v->status] : '';
//            $itemOne = (array) self::dbConn(self::$dbConfigItem)->where('id', $v->item_id)->first();
            $taskOne = (array) self::dbConn(self::$dbYwTask)->where('id', $v->task_id)->first();
            $whOne = (array) self::dbConn(self::$dbConfigWorkHour)->where('id', $v->wh_id)->first();
//            $item_name = empty($itemOne) ? '' : $itemOne['title'];
            $task_detail = empty($taskOne) ? '' : $taskOne['detail'];
            $wh_name = empty($whOne) ? '' : $whOne['title'];
            $pubtime = date('Y-m-d H:i', strtotime($v->addtime));
            $replyOne = (array) self::dbConn(self::$dbYwWhStatus)
                ->where('wh_id', $v->id)
                ->orderBy('id', 'desc')
                ->first();
            if ($replyOne['type'] == 2) {
                $reply_txt = ''; $edit_txt = $replyOne['remark'];
            } else {
                $reply_txt = $replyOne['remark']; $edit_txt = '';
            }
            $arys[] = [
                'id' => $v->id,
                'status' => $v->status, 'status_name' => $status_name,
                'title' => $wh_name, 'pubtime' => $pubtime, 'detail' => $task_detail,
                'reply_txt' => $reply_txt, 'reply_imgs' => [], 'edit_txt' => $edit_txt,
            ];
        }
        return self::toback(0, '', $arys);
    }

    //审核拒绝
    public static function authNo($id, $uid, $remark)
    {
        $whOne = (array) self::dbConn(self::$dbYwWh)->where('id', $id)->first();
        if (empty($whOne)) {
            return self::toback(-2, '没有数据');
        }
        self::dbConn(self::$dbYwWhStatus)->insertGetId([
            'wh_id' => $id, 'pub_uid' => $whOne['staff_id'],
            'auth_uid' => $uid, 'type' => 3, 'remark' => $remark,
            'addtime' => toDate(),
        ]);
        self::dbConn(self::$dbYwWh)->where('id', $id)->update(['status' => 3]);
        return self::toback();
    }

    //审核同意
    public static function authOk($id, $uid)
    {
        $whOne = (array) self::dbConn(self::$dbYwWh)->where('id', $id)->first();
        if (empty($whOne)) {
            return self::toback(-2, '没有数据');
        }
        self::dbConn(self::$dbYwWhStatus)->insertGetId([
            'wh_id' => $id, 'pub_uid' => $whOne['staff_id'],
            'auth_uid' => $uid, 'type' => 4, 'addtime' => toDate(),
        ]);
        self::dbConn(self::$dbYwWh)->where('id', $id)->update(['status' => 4]);
        return self::toback();
    }

    //重新修改
    public static function authModify($id, $uid, $remark)
    {
        $whOne = (array) self::dbConn(self::$dbYwWh)->where('id', $id)->first();
        if (empty($whOne)) {
            return self::toback(-2, '没有数据');
        }
        self::dbConn(self::$dbYwWhStatus)->insertGetId([
            'wh_id' => $id, 'pub_uid' => $whOne['staff_id'],
            'auth_uid' => $uid, 'type' => 2, 'remark' => $remark,
            'addtime' => toDate(),
        ]);
        self::dbConn(self::$dbYwWh)->where('id', $id)->update(['status' => 2]);
        return self::toback();
    }

    //回复
    public static function authReply($id, $uid, $remark)
    {
        $whOne = (array) self::dbConn(self::$dbYwWh)->where('id', $id)->first();
        if (empty($whOne)) {
            return self::toback(-2, '没有数据');
        }
        self::dbConn(self::$dbYwWhStatus)->insertGetId([
            'wh_id' => $id, 'pub_uid' => $whOne['staff_id'],
            'auth_uid' => $uid, 'type' => 5, 'remark' => $remark,
            'addtime' => toDate(),
        ]);
        self::dbConn(self::$dbYwWh)->where('id', $id)->update(['status' => 5]);
        return self::toback();
    }
}